package org.example.batchInsert;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
import java.util.UUID;

public class BatchInsertDemo {

    // 数据库连接配置
    private static final String URL = "jdbc:mysql://localhost:3306/test5";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";

    public static void main(String[] args) {
        // 注册JDBC驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        // SQL插入语句（列顺序与建表语句一致）
        String sql = "INSERT INTO bo_busi_customer (" +
                "cust_id, cust_type, cust_name, nick_name, cust_email, mobile_phone, " +
                "root_user_id, id_card_type, id_card_no, origin_channel, sale_channel, " +
                "is_agent_cust, industry_id, cust_level, consumption_level, cur_manager_id, " +
                "cur_maintain_id, status_cd, status_date, province_id, city_id, branch_id, " +
                "create_staff, create_date, update_staff, update_date, remark, department_id, " +
                "cust_cloud_type, next_follow_date, is_open_sea, is_main_account, " +
                "register_channel_type, register_channel_name, key_word, latest_clue_time" +
                ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        // 批处理大小
        final int BATCH_SIZE = 2000;
        final int TOTAL_RECORDS = 50000;

        try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            conn.setAutoCommit(false); // 关闭自动提交

            Random random = new Random();
            long currentTime = System.currentTimeMillis();

            for (int i = 1; i <= TOTAL_RECORDS; i++) {
                // 1. 主键UUID
                pstmt.setString(1, UUID.randomUUID().toString());

                // 2. 固定值/随机值生成
                pstmt.setString(2, "CUS_TYPE_" + (random.nextInt(5) + 1));
                pstmt.setString(3, "客户" + i);
                pstmt.setString(4, "昵称" + i);
                pstmt.setString(5, "email" + i + "@example.com");
                pstmt.setString(6, "138" + String.format("%08d", random.nextInt(100000000)));
                pstmt.setString(7, "USER" + random.nextInt(10000));
                pstmt.setString(8, random.nextBoolean() ? "ID_CARD" : "PASSPORT");
                pstmt.setString(9, "ID" + random.nextInt(1000000000));
                pstmt.setString(10, "CHAN" + random.nextInt(10));
                pstmt.setString(11, "SALE_CHAN" + random.nextInt(5));
                pstmt.setString(12, random.nextBoolean() ? "Y" : "N");
                pstmt.setString(13, "IND" + random.nextInt(20));
                pstmt.setString(14, "LEVEL" + random.nextInt(5));

                // 3. 消费等级随机生成 (V0-V10)
                pstmt.setString(15, "V" + random.nextInt(11));

                long currentTimeMillis = System.currentTimeMillis();

                // 4. 剩余字段
                pstmt.setString(16, "MANAGER" + random.nextInt(100));
                pstmt.setString(17, "MAINTAIN" + random.nextInt(100));
                pstmt.setString(18, "STATUS" + random.nextInt(3));
                pstmt.setLong(19, currentTimeMillis);
                pstmt.setString(20, "PROV" + random.nextInt(34));
                pstmt.setString(21, "CITY" + random.nextInt(333));
                pstmt.setString(22, "DIST" + random.nextInt(2851));
                pstmt.setString(23, "STAFF" + random.nextInt(50));
                pstmt.setLong(24, currentTimeMillis);
                pstmt.setString(25, "STAFF" + random.nextInt(50));
                pstmt.setLong(26, currentTime);
                pstmt.setString(27, "备注内容" + i);
                pstmt.setString(28, "DEPT" + random.nextInt(20));
                pstmt.setString(29, "CLOUD_TYPE" + random.nextInt(3));
                pstmt.setLong(30, currentTimeMillis);
                pstmt.setString(31, random.nextBoolean() ? "Y" : "N");
                pstmt.setString(32, random.nextBoolean() ? "Y" : "N");
                pstmt.setString(33, "REG_TYPE" + random.nextInt(5));
                pstmt.setString(34, "渠道" + random.nextInt(10));
                pstmt.setString(35, "关键词" + random.nextInt(100));
                pstmt.setLong(36, currentTimeMillis);

                pstmt.addBatch(); // 添加到批处理

                // 批量提交
                if (i % BATCH_SIZE == 0 || i == TOTAL_RECORDS) {
                    pstmt.executeBatch();
                    conn.commit();
                    System.out.println("已插入: " + i + "/" + TOTAL_RECORDS);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("数据插入完成！");
    }
}